using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventManager : MonoBehaviour
{
    //public Action<object> EnventHandler;
    //public delegate void EnventHandler(object args);
    Dictionary<int, Action<object>> m_Events=new Dictionary<int, Action<object>>();
    public void Subscribe(int id, Action<object> e)
    {
        if(m_Events.ContainsKey(id))
        {
            m_Events[id] += e;
        }
        else
        {
            m_Events.Add(id, e);
        }
    }
    public void UnSubscribe(int id,Action<object> e)
    {
        if (m_Events.ContainsKey(id))
        {
            if (m_Events[id] != null)
            {
                m_Events[id] -= e;
            }
            if (m_Events[id] == null)
            m_Events.Remove(id);
        }
    }
    public void Fire(int id,object args=null)
    {
        Action<object> handler;
        if(m_Events.TryGetValue(id, out handler))
        {
            handler(args);
        }
    }
}
